package com.trkj.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.J_CardDao;
import com.trkj.dao.jpa.J_chargeprojectDao;
import com.trkj.dao.mybatis.M_CardDao;
import com.trkj.pojos.Card;
import com.trkj.pojos.ChargeprojectTable;
import com.trkj.pojos.ChargetypeTable;
import com.trkj.pojos.DepositTable;
import com.trkj.vo.Serach_condition;

@Service
@Transactional
public class Card_service {
	@Autowired
	J_CardDao j_cardDao;
	@Autowired
	M_CardDao m_cardDao;
	@Autowired
	J_chargeprojectDao j_charge;
	
	private static String CRADID;
	private static String ADDRESS;
	private static BigDecimal AGE;
	private static String IDCARD;
	private static String NAME;
	private static String SEX;
	
	/**
	 * 判断该身份证号今天是否办理了两次卡，如果没有达到两次就可以办卡
	 * @param card
	 * @return
	 * @throws ParseException 
	 */
	public String vertifyApplyCard(Card card) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();//取时间
		Date start_date=null;
		Date end_date=null;
		try {
			start_date = format.parse(format.format(date));
			Calendar calendar = new GregorianCalendar();
			calendar.setTime(date);
			calendar.add(calendar.DATE,1);
			date=calendar.getTime(); 
			end_date=format.parse(format.format(date));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		List<Card> list=j_cardDao.findByIdCard(card.getIdCard(), start_date, end_date);
		if(list.size()>=2){
			return "对不起！一天只能办两张卡哦！";
		}
		String maxId=j_cardDao.findMaxCardId();
		String newId="";
		try {
			newId="p"+(Integer.parseInt(maxId)+1);
			Card card2=j_cardDao.findOne(newId);
			while(card2!=null){
				newId="p"+(Integer.parseInt(newId.substring(1))+1);
				card2=j_cardDao.findOne(newId);
			}
		} catch (Exception e) {
			System.out.println("数据库的最大卡号含有一位以上非数字");
		}
		load_message(card);
		CRADID=newId;
		return newId;
	}
	
	/**
	 * 获取用户输入的信息
	 * @param card
	 */
	public void load_message(Card card){
		ADDRESS=card.getAddress();
		AGE=card.getAge();
		IDCARD=card.getIdCard();
		NAME=card.getName();
		SEX=card.getSex();
	}
	
	/**
	 * 获得诊疗卡收费详情
	 * @return
	 */
	public Map<String,Object> get_card_project(){
		Map<String,Object> map=new HashMap<>();
		map.put("project", j_charge.findByChaegename("办卡"));
		map.put("card_id", CRADID);
		return map;
	}
	
	/**
	 * 生成一张新的诊疗卡
	 */
	public String applyCard(String cradId,String project_id,Double project_cost){
		Card card=new Card();
		card.setAddress(ADDRESS);
		card.setAge(AGE);
		card.setCradId(CRADID);
		card.setIdCard(IDCARD);
		card.setName(NAME);
		card.setSex(SEX);
		card.setStatus("正常");
		card.setTime(new Date());
		j_cardDao.save(card);
		return "办卡成功，卡号："+CRADID;
	}
	
	/**
	 * 多条件分页查询查看所有诊疗卡
	 * @return
	 */
	public Map<String,Object> checkAllCard(Serach_condition card){
		Page<Card> p = PageHelper.startPage(card.getPage(), card.getRows(), true);
		List<Card> list = m_cardDao.findAllCard(card);
		Map<String,Object> map=new HashMap<>();
		map.put("rows", list);
		map.put("total", p.getTotal());
		map.put("page", 1);
		return map;
	}
	
	/**
	 * 修改诊疗卡信息
	 * @param card
	 * @return
	 */
	public String save_change_card_detail(Card card){
		Card card2=j_cardDao.findOne(card.getCradId());
		card2.setAddress(card.getAddress());
		card2.setAge(card.getAge());
		card2.setIdCard(card.getIdCard());
		card2.setName(card.getName());
		return "修改成功";
	}
	
	/**
	 * 挂失诊疗卡
	 * @param card_id
	 * @return
	 */
	public String set_card_lost(String card_id){
		Card card=j_cardDao.findOne(card_id);
		card.setStatus("丢失");
		return "挂失成功";
	}
	
}
